// 引导页
$(function(){
    var storage = window.localStorage;
    if (!storage.getItem("pagekey")){
        storage.setItem("pagekey",1);
        $('.yindao').show();
        $('.yindao_close').click(function(){
            $('.yindao').hide();
        })
    }
})
// 搜索
$(function(){
	//输入框输入时
	$('.onClickSearch').click(function(){
		searchType();
	})
	$('.cancel').click(function(){
		runajax();
	})
})
//搜索框展示
function searchType() {
    var l_searchInput = $.trim($('#searchInput').val());    
    if (l_searchInput == "") {
		runajax();
    }
    if (l_searchInput){
    	var html_Str='',
    		html_Str2 = '';
    	//获取种类标签
    	if(/^[a-zA-Z]+$/.test($.trim($('#searchInput').val()))){
    		 $('.English').each(function() {
		      	var typeArr = [];
		      	var str = '';
		      	var English = $(this).html(); //品种英文
		      	var searchValsStr = $.trim($('#searchInput').val()); //用户输入的值
		      	if(/^[a-z]+$/.test(searchValsStr)) {
		      		//输入内容为小写字母  转为大写
					searchValsStr = searchValsStr.toUpperCase();
		      	}
		      	if(English.indexOf(searchValsStr) == 0) {
		      		html_Str += '<li>'+$(this).parents('li').html()+'</li>';
		      	}
	        })
    		 $('#j_ul_append').html(html_Str);
    	}else{
    		$('.Chinese').each(function() {
		      	var typeArr = [];
		      	var str = '';
		      	var Chinese = $(this).html(); //品种中文	      	
		      	var searchValsStr = $.trim($('#searchInput').val()); //用户输入的值
		      	console.log(searchValsStr+'.............'+Chinese)
		      	if(Chinese.indexOf(searchValsStr) == 0) {
		      		console.log($(this).parents('li').html())
		      		html_Str2 += '<li>'+$(this).parents('li').html()+'</li>';
		      	}
	        });
	        $('#j_ul_append').html(html_Str2);
    	} 
    }
}
// 数据调用
$(function(){
    runajax();
    if((starttime*1000) > nowtime){
        clearInterval(timer);
    }else if((endtime*1000) < nowtime){
        clearInterval(timer);
    }else{
        clearInterval(timer);
        timer = setInterval(setintervalAjax,1000);
    }
})
// 左滑删除
function prevent_default(e) {
    e.preventDefault();
}
function disable_scroll() {
    $(document).on('touchmove', prevent_default);
}
function enable_scroll() {
    $(document).unbind('touchmove', prevent_default)
}
$(function() {
   var x;
    $('.swipe-delete li > a')
        .on('touchstart', function(e) {
            $('.swipe-delete li > a').css('left', '0px') // close em all
            $(e.currentTarget).addClass('open')
            x = e.originalEvent.targetTouches[0].pageX // anchor point
        })
        .on('touchmove', function(e) {
            var change = e.originalEvent.targetTouches[0].pageX - x
            change = Math.min(Math.max(-100, change), 0) // restrict to -100px left, 0px right
            e.currentTarget.style.left = change + 'px'
            if (change < -10) disable_scroll() // disable scroll once we hit 10px horizontal slide
        })
        .on('touchend', function(e) {
            var left = parseInt(e.currentTarget.style.left)
            var new_left;
            if (left < -35) {
                new_left = '-100px'
            } else if (left > 35) {
                new_left = '100px'
            } else {
                new_left = '0px'
            }
            // e.currentTarget.style.left = new_left
            $(e.currentTarget).animate({left: new_left}, 200)
            enable_scroll()
        });
        // 删除
        $('li .delete-btn').on('touchend', function(e) {
	        e.preventDefault();
	        if(issession == ''){
	        	showTips();
		        $('.l_tips').text('用户未登录');
		        hideTips();
	        }else{
	        	var oLiPZ = $(this).parents('li');
	        	var typeNamePz = oLiPZ.attr('id');
	        	$.ajax({
		        	type:'post',
		        	url:deleteUrl,
		        	data:{typename:typeNamePz},
		        	dataType:'json',
		        	headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
		        	success:function(e){
		        		if(e.status == 0){ //用户未登录
		        			showTips();
					        $('.l_tips').text('用户未登录');
					        hideTips();
		        		}else if(e.status == 1){ //信息不正确
		        			showTips();
					        $('.l_tips').text('信息不正确');
					        hideTips();
		        		}else if(e.status == 2){ // 删除成功
		        			oLiPZ.slideUp('fast', function() {
					            $(this).remove()
					        });
		        		}else{  // 失败
		        			showTips();
					        $('.l_tips').text('删除失败');
					        hideTips();
		        		}
		        	}
		        })
			        
	        }
	    }) 
//  $('li .delete-btn').on('touchend', function(e) {
//      e.preventDefault()
//      $(this).parents('li').slideUp('fast', function() {
//          $(this).remove()
//      })
//  }) 
});
// 数据第一次调取
function runajax(){
    var rounder = (new Date()).getTime();
    var url = "http://db2015.wstock.cn/wsDB_API2/stock.php?t="+ (new Date()).getTime()+"&symbol="+MyPzList+"&query=Date,Symbol,SP1,BP1,Name&u=liyunhe1228&p=liyunhe1228&r_type=2&num=20";
    $.ajax({
      type : "get", 
      async : false, //同步请求
      url : url,
      data : '',
      timeout:1000,
      success:function(d){
      	var html = '';
      	for(var i = 0;i<d.length;i++){
            html += '<li id="'+d[i].Symbol+'" onclick="hangqing(\''+d[i].Name+'\',\''+d[i].Symbol+'\')"><div class="behind"><a href="#" class="ui-btn delete-btn" style="color:#fff;">删&nbsp;除</a></div>';
            html += '<a href="#" id="item" class="ui-btn ui-btn-icon-right ui-icon-carat-r"><div><p class="Chinese">'+d[i].Name;
            html +='<img src="'+P_zhong+d[i].Symbol.substr(2)+'.png"/></p><p class="English">'+d[i].Symbol.substr(2)+'</p></div>';
            html +='<input type="hidden" name="sell'+d[i].Symbol+'" value="'+d[i].BP1+'"/><div class="red" id="sell'+d[i].Symbol+'">';
            html +=d[i].BP1;
            html +='<img src="'+j_top+'" alt="" /></div>';
            html +='<input type="hidden" name="buy'+d[i].Symbol+'" value="'+d[i].SP1+'"/><div class="green" id="buy'+d[i].Symbol+'">';
            html +=d[i].SP1;
            html +='<img src="'+j_top+'" alt="" /></div>';
            html +='</a></li>';
      	}
      	$('#j_ul_append').html(html);
      }
    })
}
// 行情路径跳转
function hangqing(a,b){
    window.location.href = hangqingUrl+'?name='+a+'&typename='+b; 
}
//定时刷新点位
function  setintervalAjax() {
    var rounder = (new Date()).getTime();
    var url = "http://db2015.wstock.cn/wsDB_API2/stock.php?t="+ (new Date()).getTime()+"&symbol="+MyPzList+"&query=Date,Symbol,SP1,BP1,Name&u=liyunhe1228&p=liyunhe1228&r_type=2&num=20";
    $.ajax({
        type : "get",
        async : false, //同步请求
        url : url,
        data : '',
        timeout:1000,
        success:function(d){
            var html = '';
            for(var i = 0;i<d.length;i++){
                //判断品种涨跌浮动情况
                if($("input[name=sell"+d[i].Symbol+"]") && $("input[name=buy"+d[i].Symbol+"]")){
                    var selldata=$("input[name=sell"+d[i].Symbol+"]").val();
                    var buydata=$("input[name=buy"+d[i].Symbol+"]").val();
                }
                if(selldata == d[i].BP1){
                    Drefresh('sell',d[i].Symbol,'gray',d[i].BP1,d[i].BP1);
                }else if(selldata < d[i].BP1){
                	Drefresh('sell',d[i].Symbol,'red',d[i].BP1,d[i].BP1+'<img src="'+j_top+'" alt=""/>')
                }else{
                	Drefresh('sell',d[i].Symbol,'green',d[i].BP1,d[i].BP1+'<img src="'+j_down+'" alt=""/>')
                }
                if(buydata == d[i].SP1){
                	Drefresh('buy',d[i].Symbol,'gray',d[i].SP1,d[i].SP1);
                }else if(buydata < d[i].SP1){
                	Drefresh('buy',d[i].Symbol,'red',d[i].SP1,d[i].SP1+'<img src="'+j_top+'" alt=""/>')
                }else{
                	Drefresh('buy',d[i].Symbol,'green',d[i].SP1,d[i].SP1+'<img src="'+j_down+'" alt=""/>')
                }
            };
        }
    })
}
// 定时刷新小部件
function Drefresh(id,Symbol,colour,quotation,quotations){
	$('#'+Symbol+' #'+id+Symbol).attr('class',colour);
    $('#'+Symbol).find('input[name='+id+Symbol+']').val(quotation);
    $('#'+Symbol+' #'+id+Symbol).html(quotations);
// 备份
//$('#'+d[i].Symbol+' #sell'+d[i].Symbol).attr('class','gray');
//$('#'+d[i].Symbol).find('input[name=sell'+d[i].Symbol+']').val(d[i].BP1);
//$('#'+d[i].Symbol+' #sell'+d[i].Symbol).html(d[i].BP1);
}
//手机搜索事件
$(document).keypress(function (e) {
    if (e.keyCode == 13){
        searchType();
        return false;
    }
});
